use once_cell::sync::OnceCell;
use sqlx::{mysql::MySqlPoolOptions, Error, MySqlPool};

static MYSQL_POOL: OnceCell<MySqlPool> = OnceCell::new();

const SQL_TEXT_DEV: &str = "mysql://zhangds:zhangds123a@127.0.0.1:3306/db_iot_ai?time_zone=SYSTEM";
const SQL_TEXT_PRO: &str =
    "mysql://zhangds:zhangds123a@39.101.75.181:3306/db_iot_ai?time_zone=SYSTEM";

//建立mysql连接
pub async fn init_db_pool() -> Result<(), Error> {
    let pool = MySqlPoolOptions::new()
        .max_connections(5)
        .connect(SQL_TEXT_DEV)
        .await?;
    assert!(MYSQL_POOL.set(pool).is_ok());
    Ok(())
}

//获取数据库
pub fn get_pool() -> Option<&'static MySqlPool> {
    MYSQL_POOL.get()
}
